package com.whm.hrmanagement.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whm.common.core.domain.entity.SysUser;
import com.whm.common.enums.OaStaffAttendanceEnum;

import com.whm.common.utils.html.DatetimeUtil;
import com.whm.hrmanagement.domain.OaQpglqpms;
import com.whm.hrmanagement.domain.OaStaffAttendance;
import com.whm.hrmanagement.domain.OaZrsglryda;
import com.whm.hrmanagement.domain.dto.OaMenHuHead;
import com.whm.hrmanagement.domain.dto.OaMenhuBarDate;
import com.whm.hrmanagement.domain.dto.OaMenhuPieDate;
import com.whm.hrmanagement.mapper.*;
import com.whm.hrmanagement.service.IOaMenhuService;
import com.whm.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 参保城市Service业务层处理
 * 
 * @author whm
 * @date 2024-02-09
 */
@Service
public class OaMenhuServiceImpl implements IOaMenhuService
{
    @Autowired
    private OaStaffAttendanceMapper oaStaffAttendanceMapper;
@Autowired
  public SysUserMapper sysUserMapper;
    @Autowired
    public OaZrsglrydaMapper oaZrsglrydaMapper;
    @Autowired
    public OaPersonAskMapper oaPersonAskMapper;

    @Autowired
    public OaQpglqpmsMapper oaQpglqpmsMapper;



    @Override
    public OaMenHuHead getHead() {
        OaMenHuHead oaMenHuHead = new OaMenHuHead();
        System.out.println(  sysUserMapper.selectCount(null));
        oaMenHuHead.setStaffTotal(Math.toIntExact(sysUserMapper.selectCount(null)));
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("sex" ,0);
        oaMenHuHead.setMenNum(Math.toIntExact(sysUserMapper.selectCount(qw)));
        QueryWrapper<SysUser> qw2 = new QueryWrapper<>();
        qw2.eq("sex" ,1);
        oaMenHuHead.setWomenNum(Math.toIntExact(sysUserMapper.selectCount(qw2)));
        oaMenHuHead.setNeedsTotal(Math.toIntExact(oaPersonAskMapper.selectCount(null)));
         List<OaQpglqpms> oaQpglqpms=  oaQpglqpmsMapper.selectOaQpglqpmsList(new OaQpglqpms());
        LocalDate currentDate= LocalDate.now();
        oaMenHuHead.reviewTotal  = (int) oaQpglqpms.stream().filter(data->data.getMsrq().equals(currentDate)).count();
        return oaMenHuHead;
    }

    @Override
    public   List<OaMenhuPieDate> getPieData() {
        List<OaMenhuPieDate> oaMenhuPieDates = new ArrayList<>();
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("sex" ,0);
        Integer menNum = Math.toIntExact(sysUserMapper.selectCount(qw));
        oaMenhuPieDates.add(new OaMenhuPieDate("男",menNum));
        QueryWrapper<SysUser> qw2 = new QueryWrapper<>();
        qw2.eq("sex" ,1);
        Integer womenNum = Math.toIntExact(sysUserMapper.selectCount(qw2));
        oaMenhuPieDates.add(new OaMenhuPieDate("女",womenNum));
return oaMenhuPieDates;
    }

    @Override
    public List<OaMenhuBarDate> getBarData() {
        List<OaMenhuBarDate> oaMenhuBarDates = new ArrayList<>();

        QueryWrapper<OaZrsglryda> qw = new QueryWrapper<>();
        qw.eq("quanRiZhiXueLi" ,"博士研究生");
    Integer boshiNum = Math.toIntExact(oaZrsglrydaMapper.selectCount(qw));
        oaMenhuBarDates.add(new OaMenhuBarDate("博士研究生",boshiNum));
        QueryWrapper<OaZrsglryda> qw2 = new QueryWrapper<>();
        qw2.eq("quanRiZhiXueLi" ,"硕士研究生");
        Integer shuoshiNum = Math.toIntExact(oaZrsglrydaMapper.selectCount(qw2));
        oaMenhuBarDates.add(new OaMenhuBarDate("硕士研究生",shuoshiNum));

        QueryWrapper<OaZrsglryda> qw3= new QueryWrapper<>();
        qw3.eq("quanRiZhiXueLi" ,"本科");
        Integer benkeNum = Math.toIntExact(oaZrsglrydaMapper.selectCount(qw3));
        oaMenhuBarDates.add(new OaMenhuBarDate("本科",benkeNum));


        QueryWrapper<OaZrsglryda> qw4= new QueryWrapper<>();
        qw4.eq("quanRiZhiXueLi" ,"大专");
        Integer dazhuanNum = Math.toIntExact(oaZrsglrydaMapper.selectCount(qw4));
        oaMenhuBarDates.add(new OaMenhuBarDate("大专",dazhuanNum));




        return oaMenhuBarDates;
    }

    @Override
    public List<HashMap<String,Object>> getAttendanceData(Long id, String month) {

            if (month == null || month == "") {
                month = DateUtil.format(new java.util.Date(), "yyyyMM");
            }
            String[] monthDayList = DatetimeUtil.getMonthDayList(month);
            // 考勤状态表
            List<HashMap<String,Object>> list = new ArrayList<>();
            for (String day : monthDayList) {
                HashMap<String, Object> map = new HashMap<>();
                OaStaffAttendance attendance = this.oaStaffAttendanceMapper.findByStaffId(id, day);
                if (attendance == null) {
                    Date date = DateUtil.parse(day, "yyyyMMdd").toSqlDate();
                    // 如果是周末就休假
                    if (DateUtil.isWeekend(date)) {
                        map.put("message", OaStaffAttendanceEnum.LEAVE.getMessage());
                        map.put("tagType",OaStaffAttendanceEnum.LEAVE.getTagType());
                    } else {
                        map.put("message",OaStaffAttendanceEnum.NORMAL.getMessage());
                        map.put("tagType",OaStaffAttendanceEnum.NORMAL.getTagType());
                    }
                } else {
                    map.put("message",attendance.getStatus().getMessage());
                    map.put("tagType",attendance.getStatus().getTagType());
                }
                list.add(map);
            }
            return list;
        }

}
